package org.bookie.web.pages.account;

import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.form.BeanPropertySelectionModel;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.bookie.framework.CommonBasePage;
import org.bookie.model.Group;
import org.bookie.model.Role;
import org.bookie.model.User;
import org.bookie.model.security.Access;
import org.bookie.model.services.GroupDAO;
import org.bookie.model.services.UserDAO;

/**
 * Page for {@link Group} administration.
 * A {@link User} can join and exit {@link Group}s using this page.
 * 
 * @author Igor Drobiazko
 *
 */
@Access(role=Role.USER)
public abstract class UserGroups extends CommonBasePage implements PageBeginRenderListener{
	
	/**
	 * {@inheritDoc}
	 */
	public void pageBeginRender(PageEvent event) {
		User user = getUserDAO().findByEmail(
				getAuthenticationContext().getUser().getEmail());
		setUser(user);
	}
    /**
     * Get the selection model for {@link Group} options 
     * @return instance of IPropertySelectionModel
     */
	public IPropertySelectionModel getGroups(){
		return new BeanPropertySelectionModel(getGroupDAO().findAll(), "name");
	}
	
	/**
	 * Update the {@link User}s groups
	 */
	public void updateGroups(){
		setMessage(getMessages().getMessage("userGroups.confirm.message"));
	}
	
	/**
	 * Get the {@link User}
	 * @return instance of 
	 */
	public abstract User getUser();
	
	/**
	 * Set the {@link User}
	 * @param user user to set
	 */
	public abstract void setUser(User user);
	
	/**
	 * Injected User service.
	 * @return instance of UserDAO
	 */
	@InjectObject("service:bookie.model.UserDAO")
	public abstract UserDAO getUserDAO();
	
	/**
	 * Injected {@link Group} service
	 * @return instance of Group
	 */
	@InjectObject("service:bookie.model.GroupDAO")
	public abstract GroupDAO getGroupDAO();
}
